﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MND.Commands
{
    class UserCommand
    {
        public string Command { get; set; }
        public string[] Params { get; set; }
        public Users.User User { get; set; }
        public CommandBase Base { get; set; }

        public UserCommand(string input, Users.User u)
        {
            User = u;

            
            // Special case for punctuation, which is marking communication, as in .say something global.
            if (Char.IsPunctuation(input[0]))
            {
                Command = input[0].ToString();
                Params = new string[] { input.Substring(1) };
            }
            else
            {

                List<string> p = new List<string>();
                bool isInQuotes = false;
                StringBuilder sb = new StringBuilder();
                string s = String.Empty;
                foreach (char c in input)
                {
                    switch (c)
                    {
                        case '\'': // '
                            isInQuotes = !isInQuotes;
                            break;
                        case ' ':
                            if (!isInQuotes)
                            {
                                s = sb.ToString().Trim();
                                if (s.Length > 0)
                                    p.Add(s);

                                sb.Clear();
                            }
                            else
                                sb.Append(c);
                            break;
                        default:
                            sb.Append(c);
                            break;
                    }
                }
                s = sb.ToString().Trim();
                if (s.Length > 0)
                    p.Add(s);

                Command = p[0];
                p.RemoveAt(0);
                Params = p.ToArray();
            }
        }
    }
}
